package com.ln.Test;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.Button;


import java.util.ArrayList;
import java.util.List;

public class MainActivity2 extends AppCompatActivity implements UserAdapter.OnChildClickListener {
    private List<UserBean> userList = new ArrayList<>();
    private UserAdapter userAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        initFruits();
        RecyclerView recycle666 = (RecyclerView) findViewById(R.id.recycle666);

        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            list.add(String.format("第%03d条数据", i));
        }
        Button doAnim666 = (Button) findViewById(R.id.doAnim666);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false);
        recycle666.setLayoutManager(layoutManager);
        userAdapter = new UserAdapter(this, list);
        userAdapter.setListener(this);
        recycle666.setAdapter(userAdapter);
        recycle666.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.set(0, 5, 0, 5);
            }
        });
        MyItemAnimator animator = new MyItemAnimator();
        animator.setRemoveDuration(500);
        animator.setAddDuration(500);
        recycle666.setItemAnimator(animator);
//        recycle666.setItemAnimator(new MyItemAnimator());
        doAnim666.setOnClickListener(view -> {

            userAdapter.remove(2);

        });
    }

    private void initFruits() {
        for (int i = 0; i < 5; i++) {
            UserBean apple = new UserBean("Apple"+i, "age"+i);
            userList.add(apple);
        }
    }

    @Override
    public void onChildClick(int position) {
        userAdapter.add(position, "新增数据");
    }
}